<?php

require_once './include/common.inc.php';
checkLogin();

$page_title = 'jans2en furniture - Products - Product Details';
$menu_id = 5;
getMyParam('categoryid');
getMyParam('pid');
getMyParam('fid');
getMyParam('gotype');
getMyParam('new');
$new_url = '';
if ($new == 1) {
    $menu_id = 7;
    $new_url = '&new=1'; 
}
$fid = (int)$fid;

$cate_id = $fid>0 ? $fid : $categoryid;

$sql = "SELECT `id`, `name`, `pic`, `seq`, `description` FROM `category` WHERE `fid` = 0 ORDER BY `seq` ASC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $out_ary[$rs['id']] = $rs;
    if ($categoryid == $rs['id']) {
        $category_name = $rs['name'];
    }
}

// вƷid,ûвƷ
$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` GROUP BY c.`fid`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $fid_ary[] = $rs['fid'];   //ͼƬ
}

// sqlfid=0ʱεһЩֻиĲƷֻʣһ
// ˵вƷ`hidden_flag` = 0ĲƷ
$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p, `product` pr WHERE pr.`id`=p.`product_id` AND c.`id`=p.`category_id` AND c.`fid` = 0 AND pr.`hidden_flag` = 0  GROUP BY p.`category_id`";
//$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` AND c.`fid` = 0 GROUP BY p.`category_id`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	$fid2_ary[] = $rs['category_id'];   //ͼƬ
}


foreach ($out_ary as $k=>$v) {
    if (!in_array($k, $fid_ary) && !in_array($k, $fid2_ary)) {
        unset($out_ary[$k]);
    }
}


if ($HAS_NEW_ITEM) {
    $out_ary[] = array(
    'id' => '1001',
    'name' => 'New Items',
    'pic' => 'new_items.jpg',
    'seq' => '1001',
    'description' => 'Product development is a continuous process in our company'
    );
}
/*$out_ary[] = array(
'id' => 1002,
'name' => 'Specials',
'pic' => 'specials.jpg',
'seq' => '1002',
'description' => 'Jans2en furniture has recently placed the following items on sale. A limited amount of inventory is available on a first come, first served basis. Our sales are a result of discontinued inventory, over stocked items, or occasionally we may just want to su'
);*/

$sub_out_ary = array();
if ($cate_id > 0) {
    //$sql = "SELECT `id`, `name`, `pic`, `seq`, `description` FROM `category` WHERE `fid` = $cate_id ORDER BY `seq` ASC";
    $sql = "SELECT c.`id`, c.`name`, c.`pic`, c.`pic_2`, c.`seq`, c.`description` FROM `category` c, `product_category` pc WHERE c.id=pc.`category_id` AND c.`fid` = $cate_id GROUP BY c.`id` ORDER BY c.`seq` ASC";
    //echo $sql;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $sub_out_ary[] = $rs;
        $id_2_ary[] = $rs['id'];
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
        }
    }
}


if (!is_numeric($pid) || $pid <= 0) {
    header("Location:product.php");
    exit;
}

$no_preview_next = false;
$current_page = 0;
//if ($categoryid == "" || $categoryid == 0 || $categoryid > 1000) {
if ($categoryid == "" || $categoryid == 0 ) {
    $no_preview_next = true;
} else {
    if ($gotype == "preview" || $gotype == "next") {
        $pid_ary = array();
        $thisid = false;
        $new_pid = '';

        // new_item
        if ($cate_id>1000) {
            $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag` FROM `product` p, `product_category` pc, `category` c WHERE c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 GROUP BY p.`id` ORDER BY c.fid ASC, c.seq ASC, pc.seq ASC, p.`code` DESC";
            /*$query = $db->query($sql);
            while($rs = $db->fetch_array($query)) {
            $photo_ary[] = array('id'=>$rs['id'], 'name'=>$rs['name'], 'pic'=>$rs['pic'], 'code'=>$rs['code']);
            }*/
        } else {
            if ($fid <= 0 && !empty($id_2_ary)) {
                $id_2_str = implode(",", $id_2_ary);
                $sql_3 = "AND c.`category_id` IN (" . $id_2_str . ")";
            } else {
                $sql_3 = "AND c.`category_id`='$categoryid'";
            }
            $sql = "SELECT p.`id` FROM `product` p, `product_category` c, `category` ca WHERE ca.`id` = c.`category_id` AND p.`id`=c.`product_id` $sql_3 AND p.`hidden_flag`=0 GROUP BY p.`id` ORDER BY ca.seq asc, c.seq asc, p.`code` DESC, p.`id` DESC";
        }
        //echo $sql;
        //exit;

        $query = $db->query($sql);
        $index_num = 0;
        while($rs = $db->fetch_array($query)) {
            if ($new_pid == '') {
                //echo $rs['id'];echo " | " . $pid;echo "<br>";
                if ($gotype == "next" && $thisid) {
                    $new_pid = $rs['id'];
                }
                if ($rs['id'] == $pid) {
                    $thisid = true;
                    if ($gotype == "preview") {
                        $new_pid = empty($pid_ary) ? $pid : array_pop($pid_ary);
                        $index_num--;
                    }
                }
                $pid_ary[] = $rs['id'];
                //echo $new_pid;
                //echo "<pre>";print_r($pid_ary);//exit;
                $index_num++;
            }
            
        }

        $current_page = ceil($index_num/12);
        //echo $current_page;
        if ($new_pid == "") {
            $new_pid = $pid;
        }
        $pid = $new_pid;


        //echo $pid;exit;
    }
}


$sql = "SELECT * FROM `product` WHERE `id`='$pid' AND `hidden_flag`=0";
echo $sql;
$product_info_ary = $db->get_one($sql);

$clearance_flag = false;


$item_flag = '';
if ($product_info_ary['new_flag'] == 1 && $clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>New! Clearance!</em></font>";
} elseif ($product_info_ary['new_flag'] == 1) {
    $item_flag = "<font color='red' size='2'><em>New!</em></font>";
} elseif ($clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>Clearance!</em></font>";
}

echo $product_info_ary['pic'];
exit;

if (trim($product_info_ary['pic']) == "" || !file_exists("./zh/attachments/" . $product_info_ary['pic'])) {
    $product_info_ary['pic'] = 'no_big.gif';
}

//$width_inch = sprintf("%0.2f", $product_info_ary['width']/2.54);
//$height_inch = sprintf("%0.2f", $product_info_ary['height']/2.54);
//$length_inch = sprintf("%0.2f", $product_info_ary['length']/2.54);

$product_info_ary['width'] = sprintf("%0.2f", $product_info_ary['width']);
$product_info_ary['height'] = sprintf("%0.2f", $product_info_ary['height']);
$product_info_ary['length'] = sprintf("%0.2f", $product_info_ary['length']);

$width_inch = sprintf("%0.2f", $product_info_ary['width2']);
$height_inch = sprintf("%0.2f", $product_info_ary['height2']);
$length_inch = sprintf("%0.2f", $product_info_ary['length2']);

$product_info_ary['cbm'] = sprintf("%0.2f", $product_info_ary['cbm']);
$product_info_ary['cuft'] = sprintf("%0.2f", $product_info_ary['cuft']);

$remarks = $product_info_ary['comment'] == '' ? '' : "<br>" . $product_info_ary['comment'];

if ($jansen_type==2) {
    $money_type = 'USD';
    $product_info_ary['price_2'] = number_format($product_info_ary['price_2'], 2, '.', ',');
    $price_show = $product_info_ary['price_2'];
} elseif ($jansen_type == 1) {
    $money_type = 'HKD';
    $product_info_ary['price_3'] = number_format($product_info_ary['price_3'], 2, '.', ',');
    $price_show = $product_info_ary['price_3'] ;
}


if($categoryid>0 && $current_page>0) {
    $return_href = "product_list.php?categoryid=$categoryid&fid=$fid&page=$current_page";
} 
/*elseif($categoryid>0) {
    $return_href = "product_list.php?categoryid=$categoryid&fid=$fid";
}*/ 
else {
    $return_href = "javascript:history.back(1)";
}

require_once PrintEot('header');

if ((($categoryid == 0 || $categoryid == "") && ($fid == 0 || $fid == "") ) || $new) {
    require_once PrintEot('product_details3');
} else {
    require_once PrintEot('product_details3');
}
require_once PrintEot('footer');

?>